unit U_Graf_Ajuste;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, ComCtrls, Buttons;

const
  crCruz=1;
  crMaoAberta=2;
  crMaoFechada=3;
type
  TF_Graf_Ajuste = class(TForm)
    Panel1: TPanel;
    PaintBox1: TPaintBox;
    GroupBox2: TGroupBox;
    TrackBar2: TTrackBar;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    GroupBox1: TGroupBox;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    Panel2: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    GroupBox3: TGroupBox;
    Label5: TLabel;
    Button1: TButton;
    GroupBox4: TGroupBox;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    procedure grade(dx,dy:integer);
    procedure FormShow(Sender: TObject);
    procedure grafico2;
    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  F_Graf_Ajuste: TF_Graf_Ajuste;
  centrox,centroy,zoom,meiox,meioy,x1,x2,y1,y2:integer;
  res:real;
  ori,des:tpoint;
  operando,mover,coordenada:boolean;

implementation

uses unit3, Unit2, U_Aj_Pol, funcao, Ucompute;

{$R *.DFM}

procedure TF_Graf_Ajuste.grade(dx,dy:integer);
var  i,k:integer;
begin
     centrox:=centrox+dx;
     centroy:=centroy+dy;
     PaintBox1.Canvas.Rectangle(x1-1,y1-1,x2+1,y2+1);
     i:=centrox{gx_l div 2};
     PaintBox1.Canvas.Pen.Color:=clGray;
     while i<=x2 do
      begin
        paintbox1.canvas.MoveTo(i,y1);
        PaintBox1.Canvas.LineTo(i,y2);
     {   for k:=y1 to y2 do
           paintbox1.Canvas.Pixels[i,k]:=clGray;}
        i:=i+zoom;
      end;
      i:=centrox;
      while i>=x1 do
       begin
         PaintBox1.Canvas.MoveTo(i,y1);
         PaintBox1.Canvas.LineTo(i,y2);
      {   for k:=y1 to y2 do
            paintbox1.Canvas.Pixels[i,k]:=clGray;}
         i:= i-zoom;
       end;
      i:=centroy{gy_l div 2};
      while i<=y2 do
       begin
          PaintBox1.Canvas.MoveTo(x1,i);
          PaintBox1.Canvas.LineTo(x2,i);
          {for k:=x1 to x2 do
             paintbox1.Canvas.Pixels[k,i]:=clGray;}
          i:= i+zoom;
       end;
      i:=centroy;
      while i>=y1 do
       begin
          PaintBox1.Canvas.MoveTo(x1,i);
          PaintBox1.Canvas.lineto(x2,i);
     {    for k:=x1 to x2 do
            paintbox1.Canvas.Pixels[k,i]:=clGray;}
         i:=i-zoom;
       end;
        {Desenho dos eixos}
     PaintBox1.Canvas.Pen.Color:=clred;
     if (centrox>=x1) and (centrox<=x2) then
      begin
         PaintBox1.Canvas.moveto(centrox,y1);
         PaintBox1.Canvas.lineto(centrox,y2);
 {       for k:=y1 to y2 do
            paintbox1.Canvas.Pixels[centrox,k]:=clred;}
      end;
     if (centroy>=y1) and (centroy<=y2) then
      begin
        PaintBox1.Canvas.moveto(x1,centroy);
        PaintBox1.Canvas.lineto(x2,centroy);
        {for k:=x1 to x2 do
            paintbox1.Canvas.Pixels[k,centroy]:=clred;}
      end;
   { Desenho dos Pontos }
   PaintBox1.Canvas.Pen.Color:=clYellow;
   with F_Aj_Pol.Tabela do
   begin
     for i:=1 to F_Aj_Pol.SpinEdit1.value do
      begin
        PaintBox1.Canvas.Ellipse(centrox+round(zoom*(strtofloat(cells[1,i])))-2,centroy-round(zoom*(strtofloat(cells[2,i])))-2,
                                 centrox+round(zoom*(strtofloat(cells[1,i])))+3,centroy-round(zoom*(strtofloat(cells[2,i])))+3);
        PaintBox1.Canvas.Ellipse(centrox+round(zoom*(strtofloat(cells[1,i])))-1,centroy-round(zoom*(strtofloat(cells[2,i])))-1,
                                 centrox+round(zoom*(strtofloat(cells[1,i])))+2,centroy-round(zoom*(strtofloat(cells[2,i])))+2);
      end;
   end;
end;

procedure TF_Graf_Ajuste.grafico2;
var
       p,i:integer;
       x,y,{z,}j:real;
       erro1,erro2,erro3:boolean;
       g_x{, h_x}:Tfuncao;
     {  y2_str:str100;}
begin
     case trackbar2.Position of
        1: res:=1;
        2: res:=0.1;
        3: res:=0.01;
     end;

     {desenha a funcao}
     j:=x1;           { limita o desenho no eixo x }
     g_x.funcao;
   //   h_x.funcao;
     g_x.inicializa(edit1.text);
    // h_x.inicializa(form2.edit2.text);
     while j <= x2 do
      begin
         x:=(j-centrox) / zoom;
         j:=j+res;
         y:=g_x.calcula(x,erro2);
        // y:= compute(p,Edit1.text,x,erro1,erro2);
        // z:= {h_x.calcula(x,erro3);} compute(p,form2.edit2.text,x,erro1,erro3);
         if not(erro2) then
            if (centroy-y*zoom >= y1)and(centroy-y*zoom <= y2) then
             begin  {^ limita o desenho no eixo y }
 {INTEGRAL}   { linha(round(j),round(centroy-y*zoom),round(j),centroy,2);}
                paintbox1.Canvas.Pixels[round(j),round(centroy-y*zoom)]:=cllime;
     //         putpixel(round(j),round(centroy-y*zoom),2);
             end;
      {   if not(erro3) then
           if (centroy-z*zoom >= y1) and (centroy-z*zoom <= y2) then
              paintbox1.Canvas.Pixels[round(j),round(centroy-z*zoom)]:=clyellow;         }
       end;
   g_x.destroy;
  // h_x.destroy;
   { De novo o Desenho dos Pontos }
    PaintBox1.Canvas.Pen.Color:=clYellow;
   with F_Aj_Pol.Tabela do
   begin
     for i:=1 to F_Aj_Pol.SpinEdit1.value do
      begin
        PaintBox1.Canvas.Ellipse(centrox+round(zoom*(strtofloat(cells[1,i])))-2,centroy-round(zoom*(strtofloat(cells[2,i])))-2,
                                 centrox+round(zoom*(strtofloat(cells[1,i])))+3,centroy-round(zoom*(strtofloat(cells[2,i])))+3);
        PaintBox1.Canvas.Ellipse(centrox+round(zoom*(strtofloat(cells[1,i])))-1,centroy-round(zoom*(strtofloat(cells[2,i])))-1,
                                 centrox+round(zoom*(strtofloat(cells[1,i])))+2,centroy-round(zoom*(strtofloat(cells[2,i])))+2);
      end;
   end;
end;


procedure TF_Graf_Ajuste.FormShow(Sender: TObject);
begin
    SpeedButton4.Click;
    x1:=0;   x2:=399;
    y1:=0;   y2:=399;
    meiox:=(x2+x1) div 2;
    meioy:=(y2+y1) div 2;
    centrox := meiox;
    centroy := meioy;
    zoom:=16;
    res:=1;
    Edit1.Text:=F_Aj_Pol.Edit2.text;
    //Edit2.Text:=Form2.Edit2.text;

end;

procedure TF_Graf_Ajuste.PaintBox1MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var coord_x,coord_y:real;
begin
   if mover then
     if operando then
      begin
        des.x:=x;
        des.y:=y;
        grade(des.x-ori.x,des.y-ori.y);
        ori:=des;
      end;
    if coordenada then
     begin
       coord_x:=(x-centrox)/zoom;
       coord_y:=(centroy-y)/zoom;
       Label5.Caption:='(x,y)=('+formatfloat('0.##',coord_x)+','+formatfloat('0.##',coord_y)+')';
     end;
end;

procedure TF_Graf_Ajuste.PaintBox1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   if mover then
   begin
      ori.x:=x;
      ori.y:=y;
      des:=ori;
      operando:=true;
      PaintBox1.Cursor:=crMaoFechada;
   end;
end;

procedure TF_Graf_Ajuste.PaintBox1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   if mover then
    begin
     operando:=false;
     grade(0,0);
     grafico2;
     PaintBox1.Cursor:=crMaoAberta;
    end;
end;

procedure TF_Graf_Ajuste.SpeedButton1Click(Sender: TObject);
begin
     if (zoom>4) then
      begin
        centrox:=centrox - (centrox-meiox) div 2;
        centroy:=centroy - (centroy-meioy) div 2;
        zoom:=zoom div 2;
        grade(0,0);
        grafico2;
      end;
end;

procedure TF_Graf_Ajuste.SpeedButton3Click(Sender: TObject);
begin
     if (zoom<128) then
      begin
        centrox:=centrox + (centrox-meiox);
        centroy:=centroy + (centroy-meioy);
        zoom:=zoom * 2{+4;};
        grade(0,0);
        grafico2;
     end;
end;

procedure TF_Graf_Ajuste.SpeedButton2Click(Sender: TObject);
begin
     zoom:=16;
     centrox := meiox;
     centroy := meioy;
     zoom:=16;
     grade(0,0);
     grafico2;
end;

procedure TF_Graf_Ajuste.PaintBox1Paint(Sender: TObject);
begin
    grade(0,0);
    grafico2;
end;

procedure TF_Graf_Ajuste.Button1Click(Sender: TObject);
begin
   Grade(0,0);
   Grafico2;
end;

procedure TF_Graf_Ajuste.SpeedButton4Click(Sender: TObject);
begin
   PaintBox1.Cursor:=crCruz;
   coordenada:=true;
   mover:=false;
end;

procedure TF_Graf_Ajuste.FormCreate(Sender: TObject);
begin
   Screen.Cursors[crCruz]:=LoadCursorFromFile('Cruz.cur');
   Screen.Cursors[crMaoFechada]:=LoadCursorFromFile('maofechada.cur');
   Screen.Cursors[crMaoAberta]:=LoadCursorFromFile('maoaberta.cur');
end;

procedure TF_Graf_Ajuste.SpeedButton5Click(Sender: TObject);
begin
    PaintBox1.Cursor:=crMaoAberta;
    mover:=true;
    coordenada:=false;
    Label5.Caption:='(x,y)=';
end;

end.
